package org.eclipse.wb.swing;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.border.Border;

public class MenuBar_Example implements ActionListener, ItemListener {

	// define field
	private JTextArea whiteBox;
	private JCheckBoxMenuItem cake, sorbet;

	// create ContentPane , it is a Jpanel
	public Container createContentPane() {

		whiteBox = new JTextArea();
		whiteBox.setEditable(false);
		whiteBox.setWrapStyleWord(true);
		whiteBox.setLineWrap(true);
		whiteBox.setMinimumSize(new Dimension(300, 200));
		whiteBox.setPreferredSize(new Dimension(300, 200));
		whiteBox.setMaximumSize(new Dimension(300, 200));

		JScrollPane scrollPane = new JScrollPane(whiteBox);
		Border border1 = BorderFactory
				.createTitledBorder("JTextArea display actionCommand");
		scrollPane.setBorder(border1);

		JPanel contentPane = new JPanel();
		Border border = BorderFactory.createTitledBorder("This is ContentPane");
		contentPane.setBorder(border);
		contentPane.add(scrollPane);

		return contentPane;
	}

	// create MenuBar
	public JMenuBar createMenuBar() {
		// create the menubar
		JMenuBar menuBar = new JMenuBar();

		// add a menu
		// set Mnemonic is S, press ALT + S to access it as it is top level menu
		JMenu starter = new JMenu("Starter");
		starter.setMnemonic(KeyEvent.VK_S);
		menuBar.add(starter);

		// Fill the menu
		// And attact event to each component
		// soup with the Mnemoic U and Acelerator ALT + U .
		JMenuItem soup = new JMenuItem("Soup");
		soup.setMnemonic(KeyEvent.VK_U);
		soup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,
				ActionEvent.ALT_MASK));
		soup.addActionListener(this);

		JMenu steak = new JMenu("Steak");
		steak.setMnemonic(KeyEvent.VK_T);

		JMenuItem rare = new JMenuItem("Rare");
		rare.setMnemonic(KeyEvent.VK_R);
		rare.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
				ActionEvent.ALT_MASK));
		rare.addActionListener(this);

		JMenuItem welldone = new JMenuItem("WellDone");
		welldone.setMnemonic(KeyEvent.VK_W);
		welldone.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
				ActionEvent.ALT_MASK));
		welldone.addActionListener(this);

		steak.add(rare);
		steak.add(welldone);

		JRadioButtonMenuItem chips = new JRadioButtonMenuItem("Chips");
		chips.setMnemonic(KeyEvent.VK_C);
		chips.addActionListener(this);

		JRadioButtonMenuItem bp = new JRadioButtonMenuItem("Bake Potatoes");
		bp.setMnemonic(KeyEvent.VK_P);
		bp.addActionListener(this);

		ButtonGroup btnGroup = new ButtonGroup();
		btnGroup.add(chips);
		btnGroup.add(bp);

		cake = new JCheckBoxMenuItem("Cake");
		cake.setMnemonic(KeyEvent.VK_K);
		cake.addActionListener(this);

		sorbet = new JCheckBoxMenuItem("Sorbets");
		sorbet.setMnemonic(KeyEvent.VK_B);
		sorbet.addActionListener(this);

		starter.add(soup);
		starter.addSeparator();
		starter.add(steak);
		starter.addSeparator();
		starter.add(chips);
		starter.add(bp);
		starter.addSeparator();
		starter.add(cake);
		starter.add(sorbet);

		return menuBar;

	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		Object source = e.getSource();
		if (source == cake)
			whiteBox.append("cake Clicked \n");
		else if (source == sorbet)
			whiteBox.append("sorbet Clicked\n");

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		whiteBox.append(e.getActionCommand() + "	selected \n");
	}

	private static void createGUI() {

		JFrame.setDefaultLookAndFeelDecorated(true);
		JFrame frame = new JFrame("JMenuBar Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		MenuBar_Example components = new MenuBar_Example();
		frame.setContentPane(components.createContentPane());
		frame.setJMenuBar(components.createMenuBar());
		frame.setSize(400, 320);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.setVisible(true);

	}

	public static void main(String[] args) {
		Runnable runner = new Runnable() {

			@Override
			public void run() {
				createGUI();
			}
		};

		EventQueue.invokeLater(runner);
	}

}
